import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: uni.getStorageSync('userInfo')?true:false,
		userInfo: uni.getStorageSync('userInfo') || {
			avatarUrl:'/static/images/user/defaultAvatar.png',
			nickName:'点击头像登录'
		},
		buyBooks:null,
		rentBooks:null
		
	},
	mutations: {
		login(state, provider) {
			state.userInfo = JSON.parse(provider.userInfo);
			state.hasLogin = true;
			uni.setStorageSync('userInfo',JSON.parse(provider.userInfo));
			uni.setStorageSync('token',provider.token);
		},
		logout(state) {
			state.userInfo = {
				avatarUrl:'/static/images/user/defaultAvatar.png',
				nickName:'点击头像登录'
			};
			state.hasLogin = false;
			uni.removeStorageSync('userInfo');
			uni.removeStorageSync('token');
		},
		generateCarts(state,cart){
			state.buyBooks = cart.buyBooks;
			state.rentBooks = cart.rentBooks;
		}
	}
})

export default store
